package br.com.salaodebeleza.view;

import java.util.LinkedList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.inject.Inject;

import br.com.salaodebeleza.business.TbAgendaBC;
import br.com.salaodebeleza.business.TbClienteBC;
import br.com.salaodebeleza.business.TbFuncionarioBC;
import br.com.salaodebeleza.business.TbServicosBC;
import br.com.salaodebeleza.domain.TbAgenda;
import br.com.salaodebeleza.domain.TbCliente;
import br.com.salaodebeleza.domain.TbFuncionario;
import br.com.salaodebeleza.domain.TbServicos;
import br.gov.frameworkdemoiselle.annotation.PreviousView;
import br.gov.frameworkdemoiselle.stereotype.ViewController;
import br.gov.frameworkdemoiselle.template.AbstractEditPageBean;
import br.gov.frameworkdemoiselle.transaction.Transactional;

@ViewController
@PreviousView("./tbAgenda_list.jsf")
public class TbAgendaEditMB extends AbstractEditPageBean<TbAgenda, Long> {

	private static final long serialVersionUID = 1L;

	@Inject
	private FacesContext facesContext;
	
	@Inject
	private TbAgendaBC tbAgendaBC;
	
	@Inject
	private TbClienteBC tbClienteBC;
	
	@Inject
	private TbServicosBC tbServicoBC;
	
	@Inject
	private TbFuncionarioBC tbFuncionarioBC;
	
	private List<SelectItem> tbClientes = new LinkedList<SelectItem>();
	private List<SelectItem> tbServicos = new LinkedList<SelectItem>();
	private List<SelectItem> tbFuncionarios = new LinkedList<SelectItem>();
	
	@Override
	@Transactional
	public String delete() {
		this.tbAgendaBC.delete(getId());
		return getPreviousView();
	}
	
	@Override
	@Transactional
	public String insert() {
		
		try {
			this.tbAgendaBC.insert(getBean());
			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Operacao Realizada com Sucesso!", "");
			facesContext.addMessage(null, message);
		} catch (Exception e) {
			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro ao Realizar Operacao! \nEntre em contato o Administrador do Sistema.", "");
			facesContext.addMessage(null, message);
		}
		return getPreviousView();
	}
	
	
	@Override
	@Transactional
	public String update() {
		
		
		try {
			this.tbAgendaBC.update(getBean());
			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Operacao Realizada com Sucesso!", "");
			facesContext.addMessage(null, message);
		} catch (Exception e) {
			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro ao Realizar Operacao! \nEntre em contato o Administrador do Sistema.", "");
			facesContext.addMessage(null, message);
		}
		
		return getPreviousView();
	}
	
	@Override
	protected void handleLoad() {
		setBean(this.tbAgendaBC.load(getId()));
	}
	
	public List<SelectItem> getTbClientes() {

		tbClientes.clear();
		
		for(TbCliente t : tbClienteBC.findAll()){
			tbClientes.add(new SelectItem(t, t.getNmCliente()));
		}
		return tbClientes;
	}
	
	public List<SelectItem> getTbServicos() {

		tbServicos.clear();
		
		for(TbServicos t : tbServicoBC.findAll()){
			tbServicos.add(new SelectItem(t, t.getNmServico()));
		}
		return tbServicos;
	}
	
	public List<SelectItem> getTbFuncionarios() {

		tbFuncionarios.clear();
		
		for(TbFuncionario t : tbFuncionarioBC.findAll()){
			tbFuncionarios.add(new SelectItem(t, t.getNmFuncionario()));
		}
		return tbFuncionarios;
	}

}